package it.progettoso.utility;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBConnection  extends SQLiteOpenHelper { 
	
	private static final String DB_NAME="DBProgettoSO";
	private static final String TABLE_COMMAND = "commandsTable";
	private static final String TABLE_GENERALDATA = "dataApplicationTable";
	private static final int DB_VERSION=1;

	private static final String ID = "_id";
	private static final String COMMAND_KEY = "command";
	private static final String CODE_KEY = "code";
	private static final String NAME_KEY = "name";
	private static final String VALUE_KEY = "value";

	public static String INFO_COMMAND="infosmartphone";
	public static String LOCATION_COMMAND="infolocationgps";
	public static String WIFILOCATION_COMMAND="infolocationwifi";
	public static String CAM_COMMAND="infocam";
	public static String PLAYALARM_COMMAND="playalarm";
	public static String CALLME_COMMAND="callme";
	public static String LOCK_COMMAND="lock";
	public static String UNLOCK_COMMAND="unlock";

	public static String PIN="PIN";
	public static String FRIEND_NUMBER="Numero amico";
	public static String ATTEMPS="Numero tentativi";
	public static String SIM_SERIAL_NUMBER="Numero seriale";
	public static String LOCK="Lock";
	public static String EMAIL="E Mail";


	SQLiteDatabase mDb;
	static Context mContext;

	public DBConnection(Context ctx){
		super(ctx, DB_NAME, null, DB_VERSION); 
		mContext=ctx;
	}

	@Override
	public void onCreate(SQLiteDatabase _db) {

		String sql1="CREATE TABLE IF NOT EXISTS "
			+ TABLE_COMMAND + " (" 
			+ ID + " integer primary key autoincrement, "
			+ COMMAND_KEY + " text not null, "
			+ CODE_KEY + " text not null);";
		_db.execSQL(sql1);

		String sql2="CREATE TABLE IF NOT EXISTS "
			+ TABLE_GENERALDATA + " (" 
			+ ID + " integer primary key autoincrement, "
			+ NAME_KEY + " text not null, "
			+ VALUE_KEY + " text not null);";

		_db.execSQL(sql2);
	}

	public boolean open() {
		mDb=getWritableDatabase();
		if(mDb==null)
			return false;
		return true;
	}

	public void close(){ 
		mDb.close();
	}
	/* FUNZIONI DI INSERIMENTO NELLE TABELLE DEL DB*/

	public long insertCommand(String command,String code){
		ContentValues cv=new ContentValues();
		cv.put(COMMAND_KEY, command);
		cv.put(CODE_KEY, code);
		return mDb.insert(TABLE_COMMAND, null, cv);
	}


	public long insertData(String name,String value){
		ContentValues cv=new ContentValues();
		cv.put(NAME_KEY, name);
		cv.put(VALUE_KEY, value);
		return mDb.insert(TABLE_GENERALDATA, null, cv);
	}


	/* FUNZIONI DI LETTURA DALLE TABELLE DEL DB*/

	public String readCommand(String command){
		String result=null;

		String query = "SELECT "+ CODE_KEY + " FROM " + TABLE_COMMAND + " WHERE " + COMMAND_KEY +" = ?";

		String[] selectionArgs = { command };
		Cursor c = mDb.rawQuery(query, selectionArgs);
		if(c.moveToNext())
			result=c.getString(0);
		c.close();
		return result;

	}

	public String readData(String name){
		String result=null;

		String query = "SELECT "+ VALUE_KEY + " FROM " + TABLE_GENERALDATA + " WHERE " + NAME_KEY +" = ?";

		String[] selectionArgs = { name };
		Cursor c = mDb.rawQuery(query, selectionArgs);
		if(c.moveToNext())
			result=c.getString(0);
		c.close();
		return result;

	}

	/*AGGIORNAMENTO CAMPI TABELLE*/
	public void updateData(String name,String value){
		ContentValues values = new ContentValues();
		values.put(VALUE_KEY, value);
		String whereClause = NAME_KEY + " = ?";
		String[] whereArgs = { name };
		mDb.update(TABLE_GENERALDATA, values, whereClause,whereArgs);
	}
	public void updateCommand(String command,String code){
		ContentValues values = new ContentValues();
		values.put(CODE_KEY, code);
		String whereClause = COMMAND_KEY + " = ?";
		String[] whereArgs = { command };
		mDb.update(TABLE_COMMAND, values, whereClause,whereArgs);
	}
	
	
	
	public Cursor fetchProducts(){
		return mDb.query(TABLE_COMMAND, null,null,null,null,null,null);               
	}

	@Override
	public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {


	}



}